package car2017_demo;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.serialport.SerialPort;
import android.util.Log;

import com.bkrc.camera.XcApplication;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;

import car.bkrc.com.car2018.FirstActivity;
import car.bkrc.right.fragment.LeftFragment;

public class connect_transport {
    private int port = 60000;
    public static DataInputStream bInputStream = null;
    public static DataOutputStream bOutputStream = null;
    public static Socket socket = null;
    private byte[] rbyte = new byte[50];
    private Handler reHandler;
    public short TYPE = 0xAA;
    public short MAJOR = 0x00;
    public short FIRST = 0x00;
    public short SECOND = 0x00;
    public short THRID = 0x00;
    public short CHECKSUM = 0x00;
    //private String path = "/dev/ttyUSB0";
    private String path = "/dev/ttyS4";
    private int baudrate = 115200;

    private SerialPort mSerialPort = null;
    private static OutputStream SerialOutputStream;
    private InputStream SerialInputStream;
    private boolean Firstdestroy = false;  ////Firstactivity 是否已销毁了
    public int mark = -50;
    public int markC = -50;
    public byte mbyteCp[] = new byte[60];
    public Handler phHandler;
    public Context context;
    public boolean encode1_flag = false;   //用来判定是否扫描到二维码
    private long UltraSonic = 97;// 超声波
    private String UltraSonic_text = "0";// 超声波
    private long Light = 0;// 光照
    public String shapeResult_3;   //图像识别的结果
    private long CodedDisk = 0;
    private long maPan = 0, maPan1 = 0, maPan2 = 0, daoTui = 0;//  daoTui用来保存要后退的距离
    private String RFID;   //读取的RFID卡内容
    private byte RFID_text[]=new byte[60];   //读取的RFID卡内容
    public byte YY = 0x05;     //语音识别的结果
    public int CheKu = 2;     //车库信息
    public String result_qr; //二维码识别结果,
    public String color = "黄色"; //指示灯颜色
    public String openmv1_text;  //openmv识别的结果
    public String text; //暂存的结果
    public int text1, text2, text3, Light_DW;
    public byte[] openbyte = new byte[6];
    public byte[] closesbyte = new byte[6];  //用于保存报警器开关的数组
    //                                光照档位
    // 用于三次扫描测试。
    private int count = 0;

    public char str_1 = '0';  //二维码的从车信息
    public char str_2 = '0';

    public connect_transport(Context context, Handler phHandler) {
        this.context = context;
        this.phHandler = phHandler;
    }

    public void destory() {
        try {
            if (socket != null && !socket.isClosed()) {
                socket.close();
                bInputStream.close();
                bOutputStream.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void connect(Handler reHandler, String IP) {
        try {
            this.reHandler = reHandler;
            Firstdestroy = false;
            socket = new Socket(IP, port);
            bInputStream = new DataInputStream(socket.getInputStream());
            bOutputStream = new DataOutputStream(socket.getOutputStream());
            reThread.start();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void serial_connect(Handler reHandler) {
        this.reHandler = reHandler;
        try {
            mSerialPort = new SerialPort(new File(path), baudrate, 0);
            SerialOutputStream = mSerialPort.getOutputStream();
            SerialInputStream = mSerialPort.getInputStream();
            //new Thread(new SerialRunnable()).start();
            Log.e("serial_thread", "开启接受线程");
            //reThread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }


        XcApplication.executorServicetor.execute(new SerialRunnable());
        //new Thread(new serialRunnable()).start();
    }

    byte[] serialreadbyte = new byte[50];

    class SerialRunnable implements Runnable {
        @Override
        public void run() {
            while (SerialInputStream != null) {
                try {
                    int num = SerialInputStream.read(serialreadbyte);
                    // String  readserialstr =new String(serialreadbyte);
                    String readserialstr = new String(serialreadbyte, 0, num, "utf-8");
                    Log.e("----serialreadbyte----", "******" + readserialstr);
                    Log.e("serial_thread", "开始接收serialreadbyte");
                    Log.e("serial_thread", readserialstr);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = serialreadbyte;
                    reHandler.sendMessage(msg);
                    /*
                    for (int i = 0; i < num; i++) {
                        Log.e("----serialreadbyte----", "******" +Integer.toHexString(serialreadbyte[i]));
                      //  Log.e("----serialreadbyte----", "******" + serialreadbyte[i]);
                    }
                    */
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private Thread reThread = new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto1-generated method stub
            while (socket != null && !socket.isClosed()) {
                if (Firstdestroy == true)  //Firstactivity 已销毁了
                {
                    break;
                }
                try {
                    bInputStream.read(rbyte);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = rbyte;
                    reHandler.sendMessage(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

    private void send() {
        CHECKSUM = (short) ((MAJOR + FIRST + SECOND + THRID) % 256);
        // 发送数据字节数组

        final byte[] sbyte = {0x55, (byte) TYPE, (byte) MAJOR, (byte) FIRST, (byte) SECOND, (byte) THRID, (byte) CHECKSUM, (byte) 0xBB};

        if (XcApplication.isserial == XcApplication.Mode.SOCKET) {
            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        if (socket != null && !socket.isClosed()) {
                            bOutputStream.write(sbyte, 0, sbyte.length);
                            bOutputStream.flush();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.SERIAL) {

            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        SerialOutputStream.write(sbyte, 0, sbyte.length);
                        SerialOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.USB_SERIAL)
            try {
                FirstActivity.sPort.write(sbyte, 5000);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    //竞赛平台向自动评分终端返回语音识别结果
    private void send1() {
        CHECKSUM = (short) ((MAJOR + FIRST + SECOND + THRID) % 256);
        // 发送数据字节数组

        final byte[] sbyte = {(byte) 0xAF, (byte) 0X06, (byte) MAJOR, (byte) FIRST, (byte) SECOND, (byte) THRID, (byte) 0x01, (byte) 0xBF};

        if (XcApplication.isserial == XcApplication.Mode.SOCKET) {
            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        if (socket != null && !socket.isClosed()) {
                            bOutputStream.write(sbyte, 0, sbyte.length);
                            bOutputStream.flush();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.SERIAL) {

            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        SerialOutputStream.write(sbyte, 0, sbyte.length);
                        SerialOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.USB_SERIAL)
            try {
                FirstActivity.sPort.write(sbyte, 5000);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    private boolean flag_voice;  //是否合成成功

    //语音播报 合成 字符串为GBK格式,以便传输播报。
    public void voiceController(String src) {
        if (src.equals("")) {
            src = "请输入你要播报的内容";
        }
        try {
            flag_voice = true;
            byte[] sbyte = bytesend(src.getBytes("GBK"));
            send_voice(sbyte);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private byte[] bytesend(byte[] sbyte) {
        byte[] textbyte = new byte[sbyte.length + 5];
        textbyte[0] = (byte) 0xFD;
        textbyte[1] = (byte) (((sbyte.length + 2) >> 8) & 0xff);
        textbyte[2] = (byte) ((sbyte.length + 2) & 0xff);
        textbyte[3] = 0x01;// 合成语音命令
        textbyte[4] = (byte) 0x01;// 编码格式
        for (int i = 0; i < sbyte.length; i++) {
            textbyte[i + 5] = sbyte[i];
        }
        return textbyte;
    }

    //语音播报
    public void send_voice(final byte[] textbyte) {
        if (XcApplication.isserial == XcApplication.Mode.SOCKET) {
            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        if (socket != null && !socket.isClosed()) {
                            bOutputStream.write(textbyte, 0, textbyte.length);
                            bOutputStream.flush();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.SERIAL) {

            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        SerialOutputStream.write(textbyte, 0, textbyte.length);
                        SerialOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.USB_SERIAL)
            try {
                FirstActivity.sPort.write(textbyte, 5000);
            } catch (IOException e) {
                e.printStackTrace();
            }

				/*
                try {
					// ���������ֽ�����
					if (socket != null && !socket.isClosed()) {
						bOutputStream.write(textbyte, 0, textbyte.length);
						bOutputStream.flush();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				*/
    }

    //主车
    public Thread quanThread = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                quanT4();
            }
        }
    });
    //开启从车的全自动
    public Thread quanCThread = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                //quanC();
            }
        }
    });

    //二维码识别 全自动
    public void quan2() {
        switch (mark) {
            case 0:
                stop();
                yanchi(200);
                mark = 5;
                break;
            case 5:
                sheXiang(3000);
                yanchi(4000);
                mark = 6;
                break;
            case 6:
                //goq(40,250);
                yanchi(1000);
                phHandler.sendEmptyMessage(184);
                yanchi(400);
                mark = 10;
                break;
            case 10:
                yanchi(200);
                encoder_Reset();
                mark = 15;
                break;
            case 15:
                yanchi(200);
                lineq(30);
                yanchi(200);
                mark = 20;
                break;
            case 20:
                if (encode1_flag) {
                    mark = 30;
                    //line(30);
                    //mark=78;
                } else {
                    mark = 25;
                    // 添加错误机制，3次没扫描到二维码直接跳过。
                    count++;
                    if (count >= 3) {
                        mark = 30;// 获取失败，屏蔽二维码
                    }
                }
                break;
            //用于扫描二维码，后退到中心
            case 25:
                CodedDisk = mbyteCp[9] & 0xff;
                CodedDisk = CodedDisk << 8;
                CodedDisk += mbyteCp[8] & 0xff;
                maPan = (CodedDisk - 100) / 2;
                back(45, (int) maPan);// 手动设置路径；
                yanchi(3000);
                mark = 10;
                break;
            case 30:
                yanchi(200);
                go(60, 250);
                mark = 35;
                break;
            case 35:
                yanchi(200);
                phHandler.sendEmptyMessage(183);
                mark = 40;
                break;
            case 40:
                sheXiang(1500);
                mark = 45;
                break;
            default:
                break;

        }
    }

    //test--quan
    public void quanT() {
        switch (mark) {
            case 0:
                stop();
                yanchi(1000);
                mark = 1;
                break;
            case 1:
                buzzer(1);
                yanchi(1000);
                mark = 2;
                break;
            case 2:
                buzzer(0);
                yanchi(1000);
                mark = 5;
                break;
            case 5:
                digital_clear();
                yanchi(1000);
                mark = 10;
                break;
            case 10:
                digital_open();
                yanchi(1000);
                mark = 15;
                break;
            case 15:
                lineq(40);
                yanchi(1000);
                mark = 20;
                break;
            case 20:
                goq(40, 350);
                yanchi(1000);
                mark = 25;
                break;
            case 25:
                rightq(80);
                yanchi(1000);
                mark = 30;
                break;
            case 30:
                sheXiang(3000);
                yanchi(2000);
                mark = 35;
                break;
            case 35:
                phHandler.sendEmptyMessage(184);
                yanchi(1000);
                mark = 40;
                break;
            case 40:
                //encoder_Reset();
                CodedDisk = mbyteCp[9] & 0xff;
                CodedDisk = CodedDisk << 8;
                CodedDisk += mbyteCp[8] & 0xff;
                maPan1 = (CodedDisk - 100);
                yanchi(400);
                mark = 45;
                break;
            case 45:
                lineq(40);
                CodedDisk = mbyteCp[9] & 0xff;
                CodedDisk = CodedDisk << 8;
                CodedDisk += mbyteCp[8] & 0xff;
                maPan2 = (CodedDisk - 100);
                maPan = maPan2 - maPan1;
                daoTui = maPan / 3;
                yanchi(2000);
                mark = 50;
                break;
            case 50:
                if (encode1_flag) {
                    lineq(40);
                    yanchi(1000);
                    mark = 60;
                } else {
                    mark = 55;
                    // 添加错误机制，3次没扫描到二维码直接跳过。
                    count++;
                    if (count >= 2) {
                        lineq(40);
                        yanchi(1000);
                        mark = 60;// 获取失败，屏蔽二维码
                    }
                }
                break;
            //用于扫描二维码，后退到中心
            case 55:
//                back(40, 750);// 手动设置路径；
                back(40, (int) daoTui);// 手动设置路径；
                yanchi(3000);
//                mark = 40;
                mark = 50;
                break;
            case 60:
                goq(40, 350);
                yanchi(1000);
                mark = 65;
                break;
            case 65:
                phHandler.sendEmptyMessage(183);
                yanchi(200);
                mark = 70;
                break;
            case 70:
                sheXiang(1500);
                yanchi(2000);
                mark = 75;
                break;
            case 75:
                leftq(80);
                yanchi(1000);
                mark = 80;
                break;
            case 80:
                lineq(40);
                yanchi(1000);
                mark = -85;
                break;
            case 85:
                goq(40, 350);
                yanchi(1000);
                mark = 90;
                break;
            case 90:
                youJd();
                yanchi(1000);
                mark = 95;
                break;
            case 95:
                lic(2);
                yanchi(3000);
                mark = 100;
                break;
            case 100:
                rightq(80);
                yanchi(1000);
                mark = 105;
                break;
            case 105:
                lineq(40);
                yanchi(1000);
                mark = 110;
                break;
            case 110:
                goq(40, 350);
                yanchi(1000);
                mark = 115;
                break;
            case 115:
                policeController(0);
                yanchi(4000);
                mark = 120;
                break;
            case 120:
                voiceController("报警器打开成功");
                yanchi(1000);
                mark = 125;
                break;
            case 125:
                leftq(80);
                yanchi(1000);
                mark = 130;
                break;
            case 130:
                lineq(40);
                yanchi(1000);
                mark = 135;
                break;
            case 135:
                goq(40, 350);
                yanchi(1000);
                mark = 140;
                break;
            case 140:
                lineq(40);
                yanchi(1000);
                mark = 145;
                break;
            case 145:
                gear(1);
                yanchi(2000);
                mark = 150;
                break;
            case 150:
                voiceController("光照加一档");
                yanchi(1000);
                mark = 151;
                break;
            case 151:
                goq(40, 350);
                yanchi(1000);
                mark = 155;
                break;
            case 155:
                Jd180();
                yanchi(1000);
                mark = 160;
                break;
            case 160:
                lineq(40);
                yanchi(1000);
                mark = 165;
                break;
            case 165:
                goq(40, 350);
                yanchi(1000);
                mark = 170;
                break;
            case 170:
                rightq(80);
                yanchi(1000);
                mark = 175;
                break;
            case 175:
                lineq(40);
                yanchi(2000);
                mark = 180;
                break;
            case 180:
                voiceController("ECT等待闸门打开");
                yanchi(2000);
                mark = 181;
                break;
            case 181:
                goq(40, 350);
                yanchi(1000);
                mark = 182;
                break;
            case 182:
                rightq(80);
                yanchi(1000);
                lineq(40);
                yanchi(2000);
                mark = 183;
                break;
            case 183:
                phHandler.sendEmptyMessage(180);
                yanchi(2000);
                mark = 184;
                break;
            case 184:
                goq(40, 350);
                yanchi(1000);
                Jd180();
                yanchi(1000);
                lineq(40);
                yanchi(1000);
                goq(40, 350);
                yanchi(1000);
                rightq(80);
                yanchi(1000);
                mark = 185;
                break;
            case 185:
                //K_RFID();
                lineD();
                yanchi(1000);
                mark = 190;
                break;
            case 190:
                goq(40, 350);
                yanchi(1000);
                mark = 195;
                break;
            case 195:
                J_RFID();
                yanchi(1000);
                mark = 200;
                break;
            case 200:
                gate_q(0);
                yanchi(1000);
                mark = 205;
                break;
            case 205:
                voiceController("闸门打开");
                yanchi(1000);
                mark = 210;
                break;
            case 210:
                rightq(80);
                yanchi(1000);
                mark = 215;
                break;
            case 215:
                lineq(40);
                yanchi(1000);
                mark = 220;
                break;
            case 220:
                // 超声波数据
                String str = "270";
                UltraSonic = mbyteCp[5] & 0xff;
                UltraSonic = UltraSonic << 8;
                UltraSonic += mbyteCp[4] & 0xff;
                str = String.valueOf(UltraSonic);
                voiceController("测试距离为" + str);
                yanchi(2000);
                mark = 221;
                break;
            case 221:
                goq(40, 350);
                yanchi(1000);
                mark = 225;
                break;
            case 225:
                Jd180();
                yanchi(1000);
                mark = 230;
                break;
            case 230:
                lineq(40);
                yanchi(1000);
                mark = 235;
                break;
            case 235:
                goq(40, 350);
                yanchi(1000);
                mark = 240;
                break;
            case 240:
                lineq(40);
                yanchi(1000);
                mark = 250;
                break;
            case 250:
                voiceController(RFID);
                yanchi(1000);
                mark = 255;
                break;
            case 255:
                digital_close();
                yanchi(1000);
                mark = 260;
                break;
            case 260:
                goq(40, 250);
                yanchi(1000);
                mark = 265;
                break;
            case 265:
                lineB();
                yanchi(1000);
                mark = 270;
                break;
            case 270:
                goq(40, 250);
                yanchi(1000);
                mark = 275;
                break;
            case 275:
                rightq(80);
                mark = 280;
                break;
            case 280:
                lineCK();
                yanchi(1000);
                mark = 285;
                break;
            default:
                break;
        }
    }

    public void quanT2() {
        switch (mark) {
            case 0:
                stop();
                yanchi(1000);
                mark = 245;
                break;
            case 1:
                buzzer(1);
                yanchi(1000);
                mark = 2;
                break;
            case 2:
                buzzer(0);
                yanchi(1000);
                mark = 5;
                break;
            case 5:
                digital_clear();
                yanchi(1000);
                mark = 10;
                break;
            case 10:
                digital_open();
                yanchi(1000);
                mark = 15;
                break;
            case 15:
                goq(40, 350);
                yanchi(1000);
                mark = 20;
                break;
            case 20:
                lineq(40);
                yanchi(1000);
                mark = 25;
                break;
            case 25:
                goq(40, 350);
                yanchi(1000);
                mark = 30;
                break;
            case 30:
                youJd();
                yanchi(3000);
                mark = 35;
                break;
            case 35:
                shape(0);
                yanchi(3000);
                mark = 40;
                break;
            case 40:
                leftq(80);
                yanchi(1000);
                mark = 45;
                break;
            case 45:
                Distance(1);
                yanchi(2000);
                mark = 55;
                break;
            case 50:
                leftq(80);
                yanchi(1000);
                mark = 55;
                break;
            case 55:
                lineq(40);
                yanchi(1000);
                mark = 60;
                break;
            case 60:
                goq(40, 350);
                yanchi(1000);
                mark = 65;
                break;
            case 65:
                leftq(80);
                yanchi(1000);
                mark = 70;
                break;
            case 70:
                //policeController(0);
                voiceController("FRID卡内容为空");
                yanchi(3000);
                mark = 80;
                break;
            case 75:
                policeController(1);
                yanchi(1000);
                mark = 80;
                break;
            case 80:
                rightq(80);
                yanchi(1000);
                mark = 85;
                break;
            case 85:
                lineq(40);
                yanchi(1000);
                mark = 90;
                break;
            case 90:
                goq(40, 350);
                yanchi(2000);
                mark = 95;
                break;
            case 95:
                magnetic(0);
                yanchi(3000);
                mark = 100;
                break;
            case 100:
                Jd180();
                yanchi(1000);
                mark = 105;
                break;
            case 105:
                gate_q(0);
                yanchi(2000);
                mark = 110;
                break;
            case 110:
                gate(0x10, 'A', '1', '2');
                yanchi(500);
                gate(0x11, '3', 'B', '4');
                yanchi(2000);
                mark = 115;
                break;
            case 115:
                zuoJd();
                yanchi(1000);
                mark = 120;
                break;
            case 120:
                policeController(0);
                yanchi(3000);
                mark = 125;
                break;
            case 125:
                policeController(1);
                yanchi(1000);
                mark = 130;
                break;
            case 130:
                leftq(80);
                yanchi(1000);
                mark = 135;
                break;
            case 135:
                lineq(40);
                yanchi(1000);
                mark = 140;
                break;
            case 140:
                goq(40, 350);
                yanchi(1000);
                mark = 145;
                break;
            case 145:
                lineq(40);
                yanchi(1000);
                mark = 150;
                break;
            case 150:
                goq(40, 350);
                yanchi(1000);
                mark = 155;
                break;
            case 155:
                leftq(80);
                yanchi(1000);
                mark = 160;
                break;
            case 160:
                gear(1);
                yanchi(2000);
                mark = 165;
                break;
            case 165:
                Jd180();
                yanchi(1000);
                mark = 170;
                break;
            case 170:
                lineB();
                yanchi(1000);
                mark = 175;
                break;
            case 175:
                goq(40, 350);
                yanchi(1000);
                mark = 176;
                break;
            case 176:
                Traffic_light(0);
                yanchi(3000);
                mark = 177;
                break;
            case 177:
                Traffic_light(1);
                yanchi(2000);
                mark = 180;
                break;
            case 180:
                lineq(40);
                yanchi(1000);
                mark = 185;
                break;
            case 185:
                goq(40, 350);
                yanchi(1000);
                mark = 190;
                break;
            case 190:
                rightq(80);
                yanchi(1000);
                mark = 195;
                break;
            case 195:
                while (mbyteCp[2] != 0x06) {
                    Log.e("serial_thread", "ETC等待闸门");
                    String str = bytesToHexFun2(mbyteCp);
                    Log.e("serial_thread", str);
                }
                yanchi(800);
//                yanchi(3000);
                mark = 200;
                break;
            case 200:
                lineq(40);
                yanchi(1000);
                mark = 205;
                break;
            case 205:
                goq(40, 350);
                yanchi(1000);
                mark = 210;
                break;
            case 210:
                K_RFID();
                yanchi(2000);
                mark = 215;
                break;
            case 215:
                goq(40, 350);
                yanchi(2000);
                mark = 220;
                break;
            case 220:
                J_RFID();
                yanchi(1000);
                mark = 225;
                break;
            case 225:
                voiceController("1");
                yanchi(2000);
                mark = 230;
                break;
            case 230:
                rightq(80);
                yanchi(1000);
                mark = 235;
                break;
            case 235:
                lineq(40);
                yanchi(1000);
                mark = 240;
                break;
            case 240:
                goq(40, 350);
                yanchi(1000);
                mark = 245;
                break;
            case 245:
                leftq(80);
                yanchi(2000);
                mark = 250;
                break;
            case 250:
                yuYinKS();
                yanchi(2000);
                mark = 255;
                break;
            case 255:
                Jd180();
                yanchi(1000);
                mark = 260;
                break;
            case 260:
                lineq(40);
                yanchi(1000);
                mark = 265;
                break;
            case 265:
                goq(40, 350);
                yanchi(1000);
                mark = 270;
                break;
            case 270:
                leftq(80);
                yanchi(1000);
                mark = 275;
                break;
            case 275:
                lineq(40);
                yanchi(1000);
                mark = 280;
                break;
            case 280:
                goq(40, 350);
                yanchi(1000);
                mark = 285;
                break;
            case 285:
                lineCK();
                yanchi(1000);
                mark = 290;
                break;
            case 290:
                digital_close();
                yanchi(1000);
                mark = 295;
                break;
            case 295:
                stereo_garage(1);
                yanchi(2000);
                mark = 300;
                break;
            default:
                break;
        }
    }

    public void quanT3() {
        switch (mark) {
            case 0:
                stop();
                yanchi(1000);
                buzzer(1);
                yanchi(2000);
                buzzer(0);
                yanchi(1000);
                mark = 5;
                break;
            case 5:
                goq(40, 350);
                yanchi(1000);
                mark = 10;
                break;
            case 10:
                lineq(40);
                yanchi(1000);
                mark = 15;
                break;
            case 15:
                goq(40, 350);
                yanchi(1000);
                mark = 20;
                break;
            case 20:
                rightq(80);
                yanchi(2000);
                mark = 25;
                break;
            case 25:
                phHandler.sendEmptyMessage(184);
                yanchi(2000);
                voiceController(result_qr);
                yanchi(1000);
                mark = 30;
                break;
            case 30:
                leftq(80);
                yanchi(1000);
                mark = 35;
                break;
            case 35:
                lineq(40);
                yanchi(1000);
                mark = 40;
                break;
            case 40:
                goq(40, 350);
                yanchi(1000);
                mark = 45;
//                mark = 65;
                break;
            case 45:
                rightq(80);
                yanchi(2000);
                mark = 50;
                break;
            case 50:
                Traffic_light(0);
                yanchi(3000);
                mark = 51;
                break;
            case 51:
                goq(40, 550);
                yanchi(2000);
                phHandler.sendEmptyMessage(44);
                yanchi(1000);
                int i = 2;
                if (color == "黄色") {
                    i = 2;
                } else if (color == "红色") {
                    i = 1;
                } else if (color == "绿色") {
                    i = 3;
                }
                Traffic_light(i);
                yanchi(1000);
                mark = 52;
                break;
            case 52:
                goq(40, 350);
                yanchi(100);
                Jd180();
                yanchi(1000);
                lineq(40);
                yanchi(1000);
                goq(40, 350);
                yanchi(1000);
                rightq(80);
                yanchi(1000);
                lineq(40);
                mark = 70;
                break;
            case 55:
                Traffic_light(2);
                yanchi(1000);
                mark = 60;
                break;
            case 60:
                leftq(80);
                yanchi(1000);
                mark = 65;
                break;
            case 65:
                lineq(40);
                yanchi(1000);
                mark = 70;
                break;
            case 70:
                goq(40, 350);
                yanchi(1000);
                mark = 75;
                break;
            case 75:
                // 光照强度
                Light = mbyteCp[7] & 0xff;
                Light = Light << 8;
                Light += mbyteCp[6] & 0xff;
                if (Light <= 190) {
                    Light_DW = 1;
                } else if (Light > 190 && Light <= 280) {
                    Light_DW = 2;
                } else if (Light > 280 && Light <= 490) {
                    Light_DW = 3;
                } else if (Light > 490 && Light <= 650) {
                    Light_DW = 4;
                } else {
                    Light_DW = 2;
                }
                yanchi(1000);
                mark = 80;
                break;
            case 80:
                voiceController("光档为" + Light_DW);
                yanchi(1000);
                mark = 85;
                break;
            case 85:
                gear(1);
                yanchi(2000);
                mark = 90;
                break;
            case 90:
                leftq(80);
                yanchi(2000);
                mark = 100;
                break;
            case 100:
                gate_q(0);
                yanchi(1000);
                mark = 105;
                break;
            case 105:
                lineq(40);
                yanchi(1000);
                mark = 110;
                break;
            case 110:
                goq(40, 350);
                yanchi(1000);
                mark = 120;
                break;
            case 115:
                gate_q(1);
                yanchi(1000);
                mark = 120;
                break;
            case 120:
                zuoJd();
                yanchi(2000);
                mark = 125;
                break;
            case 125:
                policeController(0);
                yanchi(3000);
                mark = 130;
                break;
            case 130:
                leftq(80);
                yanchi(1000);
                mark = 135;
                break;
            case 135:
                lineq(40);
                mark = 140;
                break;
            case 140:
                goq(40, 350);
                yanchi(1000);
                mark = 145;
                break;
            case 145:
                while (mbyteCp[2] != 0x06) {
                    Log.e("serial_thread", "ETC等待闸门");
                    String str = bytesToHexFun2(mbyteCp);
                    Log.e("serial_thread", str);
                }
                yanchi(800);
                mark = 150;
                break;
            case 150:
                lineq(40);
                yanchi(1000);
                mark = 155;
                break;
            case 155:
                goq(40, 350);
                yanchi(1000);
                mark = 160;
                break;
            case 160:
                leftq(80);
                yanchi(1000);
                mark = 165;
                break;
            case 165:
                lineB();
                yanchi(1000);
                mark = 170;
                break;
            case 170:
                goq(40, 350);
                yanchi(1000);
                mark = 175;
                break;
            case 175:
                Jd180();
                yanchi(1000);
                mark = 180;
                break;
            case 180:
                lineB();
                yanchi(1000);
                mark = 185;
                break;
            case 185:
                goq(40, 350);
                yanchi(1000);
                mark = 190;
                break;
            case 190:
//                K_RFID();
                lineD();
                yanchi(1000);
                mark = 195;
                break;
            case 195:
                goq(40, 350);
                yanchi(1000);
                mark = 200;
                break;
            case 200:
                J_RFID();
                yanchi(1000);
                mark = 205;
                break;
            case 205:
                TFT_plate_number(1);
                yanchi(2000);
                mark = 210;
                break;
            case 210:
                rightq(80);
                yanchi(1000);
                mark = 215;
                break;
            case 215:
                youJd();
                yanchi(1000);
                mark = 220;
                break;
            case 220:
                lic(2);
                yanchi(2000);
                mark = 225;
                break;
            case 225:
                leftq(80);
                yanchi(1000);
                mark = 230;
                break;
            case 230:
                lineq(40);
                yanchi(1000);
                mark = 235;
                break;
            case 235:
                goq(40, 350);
                yanchi(1000);
                mark = 240;
                break;
            case 240:
                rightq(80);
                yanchi(1000);
                mark = 245;
                break;
            case 245:
                lineq(40);
                yanchi(1000);
                mark = 250;
                break;
            case 250:
                goq(40, 350);
                yanchi(1000);
                mark = 255;
                break;
            case 255:
                leftq(80);
                yanchi(1000);
                mark = 260;
                break;
            case 260:
                lineq(40);
                yanchi(1000);
                //markC=0;
                mark = 265;
                break;
            case 265:
                goq(40, 350);
                yanchi(1000);
                mark = 266;
                break;
            case 266:
                Jd180();
                yanchi(1000);
                mark = 270;
                break;
            case 270:
                //lineCK();
                back(100, 1200);
                yanchi(1000);
                mark = 275;
                break;
            case 275:
                stereo_garage(1);
                yanchi(1000);
                mark = 280;
                break;
            case 280:
                CC();
                yanchi(1000);
                mark = 285;
                break;
            default:
                break;
        }
    }

    //保存文件
    public void test() {
        switch (mark) {
            case 0:
                stop();
                yanchi(800);
                mark = 55;
                break;
            case 1:
                lineq(40);
                yanchi(1000);
                mark = 3;
                break;
            case 3:
                goq(40, 350);
                yanchi(2000);
                mark = 5;
                break;
            case 5:
                phHandler.sendEmptyMessage(184);
                yanchi(2000);
                mark = 10;
                break;
            case 10:
                String str;
                str = result_qr.substring(1, 2);
                text = result_qr.substring(3, 4);
                text = str + text;
                text1 = Integer.parseInt("00") / 10 * 16
                        + Integer.parseInt("00") % 10;
                text2 = Integer.parseInt("00") / 10 * 16
                        + Integer.parseInt("00") % 10;
                text3 = Integer.parseInt(text) / 10 * 16
                        + Integer.parseInt(text) % 10;
                digital(2, text1, text2, text3);
                yanchi(2000);
                mark = 15;
                break;
            case 15:
                leftq(80);
                yanchi(1000);
                mark = 20;
                break;
            case 20:
                lineq(40);
                yanchi(3000);
                mark = 25;
                break;
            case 25:
                // 光照强度
                Light = mbyteCp[7] & 0xff;
                Light = Light << 8;
                Light += mbyteCp[6] & 0xff;
                if (Light <= 190) {
                    Light_DW = 1;
                } else if (Light > 190 && Light <= 280) {
                    Light_DW = 2;
                } else if (Light > 280 && Light <= 490) {
                    Light_DW = 3;
                } else if (Light > 490 && Light <= 650) {
                    Light_DW = 4;
                } else {
                    Light_DW = 2;
                }
                yanchi(1000);
                mark = 30;
                break;
            case 30:
                voiceController("光照档位" + Light_DW);
                yanchi(1000);
                mark = 35;
                break;
            case 50:
                phHandler.sendEmptyMessage(44);
                yanchi(1000);
                mark = 55;
                break;
            case 54:
                yanchi(1000);
                markC = 0;
                mark = -50;
            case 55:
                lineD();
                yanchi(1000);
                mark = -60;
                break;
            case 60:
                phHandler.sendEmptyMessage(11);
                yanchi(1000);
                mark = 65;
                break;
            default:
                break;
        }
    }

    public void quanT4() {
        switch (mark) {
            case 0:
                stop();
                yanchi(1000);
                mark = 5;
                break;
            case 5:
                digital_open();
                yanchi(1000);
                mark=10;
                break;
            case 10:
                goq(40,350);
                yanchi(1000);
                mark=15;
                break;
            case 15:
                lineq(40);
                yanchi(1000);
                mark=20;
                break;
            case 20:
                sheXiang(3000);
                yanchi(3000);
                mark=35;
                break;
            case 35:
                phHandler.sendEmptyMessage(180);
                yanchi(2000);
               TFT_LCD(0x20, 'B', '5', '6');
               yanchi(500);
               TFT_LCD(0x21, '7', 'C', '8');
                yanchi(1000);
                mark=36;
                break;
            case 36:
                goq(40,400);
                yanchi(1000);
                sheXiang(2000);
                yanchi(2000);
                mark=41;
                break;
            case 41:
                youJd();
                yanchi(1000);
                mark=42;
                break;
            case 42:
                shapeSB();
                yanchi(3000);
                mark=43;
                break;
            case 43:
                rightq(80);
                yanchi(1000);
                mark=45;
                break;
            case 45:
                lineq(40);
                yanchi(1000);
                mark=50;
                break;
            case 50:
                goq(40,350);
                yanchi(1000);
                mark=55;
                break;
            case 55:
                sheXiang(1500);
                yanchi(2000);
                mark=56;
                break;
            case 56:
                Traffic_light(0);
                yanchi(3000);
                mark=60;
                break;
            case 60:
                phHandler.sendEmptyMessage(44);
                yanchi(2000);
                mark=65;
                break;
            case 65:
                int i = 2;
                if (color == "黄色") {
                    i = 2;
                } else if (color == "红色") {
                    i = 1;
                } else if (color == "绿色") {
                    i = 3;
                }
                Traffic_light(i);
                yanchi(1000);
                mark=70;
                break;
            case 70:
                lineD();
                yanchi(1000);
                mark=75;
                break;
            case 75:
                sheXiang(2000);
                yanchi(3000);
                mark=90;
                break;
            case 90:
                UltraSonic_text = String.valueOf(UltraSonic);
                text1 = Integer.parseInt("00") / 10 * 16
                        + Integer.parseInt("00") % 10;
                text2 = Integer.parseInt("00") / 10 * 16
                        + Integer.parseInt("00") % 10;
                text3 = Integer.parseInt(UltraSonic_text) / 10 * 16
                        + Integer.parseInt(UltraSonic_text) % 10;
                digital(2, text1, text2, text3);
                yanchi(2000);
                mark=95;
                break;
            case 95:
                goq(40,350);
                yanchi(1000);
                mark=100;
                break;
            case 100:
                leftq(80);
                yanchi(1000);
                mark=105;
                break;
            case 105:
                lineq(40);
//                lineD();
                yanchi(1000);
                mark=110;
                break;
            case 110:
                goq(40,380);
                yanchi(1000);
                mark=115;
                break;
            case 115:
                rightq(80);
                yanchi(2000);
                mark=125;
                break;
            case 125:
                Jd180();
                yanchi(2000);
                mark=130;
                break;
            case 130:
                lineB();
                yanchi(1000);
                mark=135;
                break;
            case 135:
                gate_q(0);
                yanchi(1000);
                gate(0x10, 'C', '6', '7');
                yanchi(500);
                gate(0x11, '8', 'G', '1');
                yanchi(1000);
                mark=140;
                break;
            case 140:
                goq(40,350);
                yanchi(2000);
                mark=150;
                break;
            case 150:
                if (YY==(byte)0x02){
                    speech(2);
                }else if (YY==(byte)0x03){
                    speech(3);
                }else if (YY==(byte)0x04){
                    speech(4);
                }else if (YY==(byte)0x05){
                    speech(5);
                }else if (YY==(byte)0x06){
                    speech(6);
                }else {
                    speech(5);
                }
                yanchi(1000);
                mark=155;
                break;
            case 155:
                rightq(80);
                yanchi(1000);
                mark=190;
                break;
            case 190:
                lineq(40);
                yanchi(1000);
                mark=195;
                break;
            case 195:
                goq(40,350);
                yanchi(1000);
                mark=200;
                break;
            case 200:
                rightq(80);
                yanchi(1800);
                mark=205;
                break;
            case 205:
                //寻迹过ETC
                lineETC();
                yanchi(1800);
                mark=215;
                break;
            case 215:
                goq(40,380);
                yanchi(1000);
                mark=220;
                break;
            case 220:
                leftq(80);
//                rightq(80);
                yanchi(1000);
                mark=221;
                break;
            case 221:
                stereo_garage(0);
                yanchi(4000);
                mark=224;
                break;
            case 224:
                Jd180();
                yanchi(2000);
                mark=225;
                break;
            case 225:
                backq(80,1200);
                yanchi(1000);
                mark=226;
                break;
            case 226:
                magnetic(0);
                yanchi(1000);
                mark=230;
                break;
            case 230:
                digital_close();
                yanchi(1000);
                mark=235;
                break;
            case 235:
                stereo_garage(1);
                yanchi(1000);
                mark=240;
                break;
            case 240:
                buzzer(1);
                yanchi(2000);
                buzzer(0);
                yanchi(1000);
                mark=245;
                break;
            case 245:
                light(1, 0);
                yanchi(1000);
                light(0, 0);
                yanchi(1000);
                light(1, 0);
                yanchi(1000);
                light(0, 0);
            default:
                break;
        }
    }

    //开始读RFID卡数据
    public void K_RFID() {
        MAJOR = (byte) 0x75;
        FIRST = (byte) (0x00);
        SECOND = (byte) (0x00);
        THRID = (byte) (0x00);
        send();
        while (mbyteCp[2] != 1 && mbyteCp[2] != 4) {
            Log.i("serial_thread", "RFID卡数据");
//            String str = bytesToHexFun2(mbyteCp);
//            Log.e("serial_thread", str);
        }
    }

    //接收RFID卡数据
    public void J_RFID() {
        MAJOR = (byte) 0x74;
        FIRST = (byte) (0x00);
        SECOND = (byte) (0x00);
        THRID = (byte) (0x00);
        send();
        while (mbyteCp[2] != 7) {
            Log.i("serial_thread", "RFID卡数据");
//            String str = bytesToHexFun2(mbyteCp);
//            Log.e("serial_thread", str);
        }
//        String str = bytesToHexFun2(mbyteCp);
//        RFID = str;
//        try {
//            new FileService().saveToSDCard(Global.RFID + ".txt", str);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    // 码盘清零
    public void encoder_Reset() {
        MAJOR = 0x07;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        yanchi(1000);
    }

    //从车全自动
    public void CC() {
        MAJOR = 0x91;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 0x23) {
            Log.e("serial_thread", "CC");
        }

    }
    //根据二维码来判断从车信息
    public void CongCheXingxi() {
        char str3[]=new char[10];
        int i=StringToCoordinate(str3,result_qr);
        byte byt=0x00;
        byt= (byte) i;
        str_1=str3[0];
        str_2=str3[0];
        switch (str_1) {
            case '1':
                switch (str_2) {
                    case 'a':
                    case 'A':
                        CC2((byte)0x01,byt);
                        break;
                    case 'b':
                    case 'B':
                        CC2((byte)0x02,byt);
                        break;
                    case 'c':
                    case 'C':
                        //CC2(0x03,);
                        break;
                    case 'd':
                    case 'D':
                        CC2((byte)0x03,byt);
                        break;
                    case 'e':
                    case 'E':
                        //CC2(0x04,);
                        break;
                    case 'f':
                    case 'F':
                        CC2((byte)0x04,byt);
                        break;
                    case 'g':
                    case 'G':
                        //CC2((byte)0x04,byt);
                        break;
                }
                break;
            case '2':
                switch (str_2) {
                    case 'a':
                    case 'A':
                        CC2((byte)0x05,byt);
                        break;
                    case 'b':
                    case 'B':
                        CC2((byte)0x06,byt);
                        break;
                    case 'c':
                    case 'C':
                        CC2((byte)0x07,byt);
                        break;
                    case 'd':
                    case 'D':
                        CC2((byte)0x08,byt);
                        break;
                    case 'e':
                    case 'E':
                        CC2((byte)0x09,byt);
                        break;
                    case 'f':
                    case 'F':
                        CC2((byte)0x10,byt);
                        break;
                    case 'g':
                    case 'G':
                        CC2((byte)0x11,byt);
                        break;
                }
                break;
            case '3':
                switch (str_2) {
                    case 'a':
                    case 'A':
                        //CC2((byte)0x11,byt);
                        break;
                    case 'b':
                    case 'B':
                        CC2((byte)0x12,byt);
                        break;
                    case 'c':
                    case 'C':
                        //CC2((byte)0x12,byt);
                        break;
                    case 'd':
                    case 'D':
                        CC2((byte)0x13,byt);
                        break;
                    case 'e':
                    case 'E':
                        break;
                    case 'f':
                    case 'F':
                        CC2((byte)0x14,byt);
                        break;
                    case 'g':
                    case 'G':
                        //((byte)0x14,byt);
                        break;
                }
                break;
            case '4':
                switch (str_2) {
                    case 'a':
                    case 'A':
                        CC2((byte)0x15,byt);
                        break;
                    case 'b':
                    case 'B':
                        CC2((byte)0x16,byt);
                        break;
                    case 'c':
                    case 'C':
                        CC2((byte)0x17,byt);
                        break;
                    case 'd':
                    case 'D':
                        CC2((byte)0x18,byt);
                        break;
                    case 'e':
                    case 'E':
                        CC2((byte)0x19,byt);
                        break;
                    case 'f':
                    case 'F':
                        CC2((byte)0x20,byt);
                        break;
                    case 'g':
                    case 'G':
                        CC2((byte)0x21,byt);
                        break;
                }
                break;
            case '5':
                switch (str_2) {
                    case 'a':
                    case 'A':
                        //CC2((byte)0x21,byt);
                        break;
                    case 'b':
                    case 'B':
                        CC2((byte)0x22,byt);
                        break;
                    case 'c':
                    case 'C':
                       // CC2((byte)0x22,byt);
                        break;
                    case 'd':
                    case 'D':
                        CC2((byte)0x23,byt);
                        break;
                    case 'e':
                    case 'E':
                        break;
                    case 'f':
                    case 'F':
                        CC2((byte)0x24,byt);
                        break;
                    case 'g':
                    case 'G':
                        break;
                }
                break;
            case '6':
                switch (str_2) {
                    case 'a':
                    case 'A':
                        CC2((byte)0x25,byt);
                        break;
                    case 'b':
                    case 'B':
                        CC2((byte)0x26,byt);
                        break;
                    case 'c':
                    case 'C':
                        CC2((byte)0x27,byt);
                        break;
                    case 'd':
                    case 'D':
                        CC2((byte)0x28,byt);
                        break;
                    case 'e':
                    case 'E':
                        CC2((byte)0x29,byt);
                        break;
                    case 'f':
                    case 'F':
                        CC2((byte)0x30,byt);
                        break;
                    case 'g':
                    case 'G':
                        CC2((byte)0x31,byt);
                        break;
                }
                break;
            case '7':
                switch (str_2) {
                    case 'a':
                    case 'A':
                        CC2((byte)0x32,byt);
                        break;
                    case 'b':
                    case 'B':
                        break;
                    case 'c':
                    case 'C':
                        break;
                    case 'd':
                    case 'D':
                        CC2((byte)0x34,byt);
                        break;
                    case 'e':
                    case 'E':
                        break;
                    case 'f':
                    case 'F':
                        CC2((byte)0x35,byt);
                        break;
                    case 'g':
                    case 'G':
                        break;
                }
                break;
        }
    }

    public void CC2(byte byte1, byte byte2) {
        MAJOR = 0x92;
        FIRST = 0x00;
        SECOND = 0x00;  //车头朝向
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 0x23) {
            Log.e("serial_thread", "CC");
//            String str = bytesToHexFun2(mbyteCp);
//            Log.e("serial_thread", str);
        }
//        String str = bytesToHexFun2(mbyteCp);
//        Log.e("serial_thread", str);
//        Log.e("serial_thread", "完成从车讯继");
    }

    //让从车发送消息
    public void AGVCS() {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = 0x80;
        FIRST = 0x01;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    //让从车关闭发送消息
    public void AGVCG() {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = 0x80;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    //给RAM发送我要接受从车数据
    public void JieshuC() {
        MAJOR = 0x80;
        FIRST = 0x01;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    //给RAM发送我要关闭接受从车数据
    public void GuanBiC() {
        MAJOR = 0x80;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    // 循迹
    public void lineq(int sp_n) {  //寻迹
        MAJOR = 0x06;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 1 && mbyteCp[2] != 4) {
            Log.i("serial_thread", "line");
        }
//        String str = bytesToHexFun2(mbyteCp);
//        Log.e("serial_thread", str);
    }

    // 循迹过白线
    public void lineB() {  //寻迹
        MAJOR = 0x76;
//        FIRST = (byte) (sp_n & 0xFF);
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 1 && mbyteCp[2] != 4) {
            Log.i("serial_thread", "lineB");
        }
    }
    // 循迹过白线
    public void lineETC() {  //寻迹
        MAJOR = 0x93;
//        FIRST = (byte) (sp_n & 0xFF);
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 1 && mbyteCp[2] != 4) {
            Log.i("serial_thread", "lineB");
        }
    }

    // 循迹读卡（简单的）
    public void lineD() {  //寻迹
        MAJOR = 0x78;
//        FIRST = (byte) (sp_n & 0xFF);
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 1 && mbyteCp[2] != 4) {
            Log.i("serial_thread", "lineB");
        }
        RFID_text=mbyteCp;
    }

    // 循迹过车库
    public void lineCK() {  //寻迹
        MAJOR = 0x77;
//        FIRST = (byte) (sp_n & 0xFF);
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 1 && mbyteCp[2] != 4) {
            Log.i("serial_thread", "lineB");
        }
    }

    // 循迹从车
    public void lineC(int sp_n) {  //寻迹
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = 0x06;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 0x00) {
            Log.i("serial_thread", "lineC123");
            String str = bytesToHexFun2(mbyteCp);
            Log.e("serial_thread", str);
        }
//        String str = bytesToHexFun2(mbyteCp);
//        Log.e("serial_thread", str);
//        yanchi(4000);
    }

    // 前进
    public void goq(int sp_n, int en_n) {
        MAJOR = 0x02;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
        while (mbyteCp[2] != 3) {
            Log.e("serial_thread", "go");
        }
    }

    // 前进从车
    public void goC(int sp_n, int en_n) {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = 0x02;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
        TYPE = type;
        // JieshuC();
        while (mbyteCp[2] != 3) {
            Log.e("serial_thread", "goC");
//            String str = bytesToHexFun2(mbyteCp);
//            Log.e("serial_thread", str);
        }
//        String str = bytesToHexFun2(mbyteCp);
//        Log.e("serial_thread", str);
//        yanchi(2000);
    }

    // 左转
    public void leftq(int sp_n) {
        MAJOR = 0x04;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 2) {
            Log.e("serial_thread", "left");
        }
    }

    // 左转从车
    public void leftC(int sp_n) {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = 0x04;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
        while (mbyteCp[2] != 2) {
            Log.e("serial_thread", "left");
        }
//        yanchi(3000);
    }

    // 右转
    public void rightq(int sp_n) {
        MAJOR = 0x05;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
        while (mbyteCp[2] != 2) {
            Log.e("serial_thread", "right");
        }
    }

    // 右转
    public void rightC(int sp_n) {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = 0x05;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
        while (mbyteCp[2] != 2) {
            Log.e("serial_thread", "right");
        }
//        yanchi(3000);
    }

    // 左转特定角度(45)
    public void zuoJd() {
        MAJOR = (byte) 0x81;
        FIRST = (byte) 0x00;
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
        yanchi(2000);
        return;
    }

    // 左转特定角度(90)
    public void zuoJd90() {
        MAJOR = (byte) 0x81;
        FIRST = (byte) 0x01;
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
        yanchi(2000);
        return;
    }

    // 右转特定角度(45)
    public void youJd() {
        MAJOR = (byte) 0x82;
        FIRST = (byte) 0x00;
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
        yanchi(2000);
        return;
    }

    // 右转特定角度(90)
    public void youJd90() {
        MAJOR = (byte) 0x82;
        FIRST = (byte) 0x01;
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
        yanchi(2000);
        return;
    }

    // 右转特定角度-180
    public void Jd180() {
        MAJOR = (byte) 0x84;
        FIRST = (byte) 0x00;
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
        yanchi(2000);
        return;
    }

    //   <<---------老方法--------->>
    // 前进
    public void go(int sp_n, int en_n) {
        MAJOR = 0x02;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();

    }

    // 后退
    public void back(int sp_n, int en_n) {
        MAJOR = 0x03;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
    }

    // 后退
    public void backq(int sp_n, int en_n) {
        MAJOR = 0x03;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
        while (mbyteCp[2] != 3) {
            Log.e("serial_thread", "backq");
        }
    }

    //左转
    public void left(int sp_n, int en_n) {
        if (FirstActivity.coded_control_flag)  //码盘控制
        {
            MAJOR = 0x04;
        } else   //角度控制
        {
            MAJOR = 0x08;
        }
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
    }

    // 右转
    public void right(int sp_n, int en_n) {

        if (FirstActivity.coded_control_flag)  //码盘控制
        {
            MAJOR = 0x05;
        } else   //角度控制
        {
            MAJOR = 0x09;
        }
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
    }

    // 停车
    public void stop() {
        MAJOR = 0x01;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    // 循迹
    public void line(int sp_n) {  //寻迹
        MAJOR = 0x06;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
        ;
    }

    //清除码盘值
    public void clear() {
        MAJOR = 0x07;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }
    //   <<---------老方法 end--------->>

    public void sheXiang(int i) {
        //摄像头左转
        if (i == 3000) {
            LeftFragment.cameraCommandUtil.postHttp(FirstActivity.IPCamera, 35, 0);
        }
        // 摄像头初始化
        if (i == 1000) {
            LeftFragment.cameraCommandUtil.postHttp(FirstActivity.IPCamera, 25, 0);
        }
        // 摄像头右转   前
        if (i == 2000) {
            LeftFragment.cameraCommandUtil.postHttp(FirstActivity.IPCamera, 33, 0);
        }
        //摄像头向前  上
        if (i == 1500) {
            LeftFragment.cameraCommandUtil.postHttp(FirstActivity.IPCamera, 31, 0);
        }
    }

    public void vice(int i) {//主从车状态转换
        if (i == 1) {//从车状态
            TYPE = 0x02;
            MAJOR = 0x80;
            FIRST = 0x01;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            yanchi(500);

            TYPE = (byte) 0xAA;
            MAJOR = 0x80;
            FIRST = 0x01;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            TYPE = 0x02;
        } else if (i == 2) {// 主车状态
            TYPE = 0x02;
            MAJOR = 0x80;
            FIRST = 0x00;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            yanchi(500);

            TYPE = (byte) 0xAA;
            MAJOR = 0x80;
            FIRST = 0x00;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            TYPE = 0xAA;
        }

    }

    //报警器
    public void policeController(int i) {
        //开
        if (i == 0) {
            infrared((byte) 0x03, (byte) 0x05,
                    (byte) 0x14, (byte) 0x45, (byte) 0xDE,
                    (byte) 0x92);
            //关
        } else if (i == 1) {
            infrared((byte) 0x67, (byte) 0x34,
                    (byte) 0x78, (byte) 0xA2, (byte) 0xFD,
                    (byte) 0x27);
        }
    }

    // 红外报警
    public void infrared(byte one, byte two, byte thrid, byte four, byte five,
                         byte six) {
        MAJOR = 0x10;
        FIRST = one;
        SECOND = two;
        THRID = thrid;
        send();
        yanchi(500);
        MAJOR = 0x11;
        FIRST = four;
        SECOND = five;
        THRID = six;
        send();
        yanchi(500);
        MAJOR = 0x12;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        yanchi(1000);
    }

    // 双色led灯
    public void lamp(byte command) {
        MAJOR = 0x40;
        FIRST = command;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    //{ "左亮", "全亮", "右亮", "全灭" };
    //   (1, 0)  (1, 1)   (0, 1)   (0, 0)
    // 指示灯
    public void light(int left, int right) {
        if (left == 1 && right == 1) {
            MAJOR = 0x20;
            FIRST = 0x01;
            SECOND = 0x01;
            THRID = 0x00;
            send();
        } else if (left == 1 && right == 0) {
            MAJOR = 0x20;
            FIRST = 0x01;
            SECOND = 0x00;
            THRID = 0x00;
            send();
        } else if (left == 0 && right == 1) {
            MAJOR = 0x20;
            FIRST = 0x00;
            SECOND = 0x01;
            THRID = 0x00;
            send();
        } else if (left == 0 && right == 0) {
            MAJOR = 0x20;
            FIRST = 0x00;
            SECOND = 0x00;
            THRID = 0x00;
            send();
        }
    }

    //{ "开", "关" };
    // 蜂鸣器
    public void buzzer(int i) {
        if (i == 1)
            FIRST = 0x01;
        else if (i == 0)
            FIRST = 0x00;
        MAJOR = 0x30;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    // { "上翻", "下翻" };
    //数码相框
    public void picture(int i) {// 图片上翻下翻
        if (i == 1)
            MAJOR = 0x50;
        else
            MAJOR = 0x51;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    public void gear(int i) {// 光照档位加
        if (i == 1)
            MAJOR = 0x61;
        else if (i == 2)
            MAJOR = 0x62;
        else if (i == 3)
            MAJOR = 0x63;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    public void fan() {// 风扇
        MAJOR = 0x70;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    //形状识别后，将识别的结果立体投影
    public void shapeSB() {
        int which;
        String[] shape_item = {"矩形", "圆形", "三角形"};
        if (shape_item[0].indexOf(shapeResult_3) != -1) {
            //矩形
            which = 0;
            data[0] = 0x12;
            data[1] = (byte) (which + 0x01);
            infrared_stereo(data);
            Log.e("shape", "===矩形");
        } else if (shape_item[1].indexOf(shapeResult_3) != -1) {
            //圆形
            which = 1;
            data[0] = 0x12;
            data[1] = (byte) (which + 0x01);
            infrared_stereo(data);
            Log.e("shape", "===圆形");
        } else if (shape_item[2].indexOf(shapeResult_3) != -1) {
            //三角形
            which = 2;
            data[0] = 0x12;
            data[1] = (byte) (which + 0x01);
            infrared_stereo(data);
            Log.e("shape", "===三角形");
        } else {
            //不是以上图形，给他个圆形。
            which = 1;
            data[0] = 0x12;
            data[1] = (byte) (which + 0x01);
            infrared_stereo(data);
            Log.e("shape", "===矩形");
        }
    }

    private short[] data = {0x00, 0x00, 0x00, 0x00, 0x00};  //立体投影所需要的

    //{ "红色", "绿色", "蓝色", "黄色", "紫色", "青色", "黑色", "白色" };
    //  0          1       2
    //立体显示--颜色
    public void color(int i) {
        data[0] = 0x13;
        data[1] = (short) (i + 0x01);
        infrared_stereo(data);
    }

    //{ "矩形", "圆形", "三角形", "菱形", "梯形", "饼图", "靶图","条形图" };
    //立体显示--形状；
    public void shape(int i) {
        data[0] = 0x12;
        data[1] = (short) (i + 0x01);
        infrared_stereo(data);
    }

    //{ "10cm", "15cm", "20cm", "28cm", "39cm" };
    //立体显示--距离；
    public void dis(int i) {
        String[] road_item = {"10cm", "15cm", "20cm", "28cm", "39cm"};
        int disNum = Integer.parseInt(road_item[i]
                .substring(0, 2));
        data[0] = 0x11;
        data[1] = (short) (disNum / 10 + 0x30);
        data[2] = (short) (disNum % 10 + 0x30);
        infrared_stereo(data);
    }

    //立体显示--车牌；
    public void lic(int i) {
        String[] lic_item = {"N300Y7A4", "N600H5B4", "N400Y6G6",
                "J888B8C8"};
        short[] li = StringToBytes(lic_item[i]);
        data[0] = 0x20;
        data[1] = (short) (li[0]);
        data[2] = (short) (li[1]);
        data[3] = (short) (li[2]);
        data[4] = (short) (li[3]);
        infrared_stereo(data);
        data[0] = 0x10;
        data[1] = (short) (li[4]);
        data[2] = (short) (li[5]);
        data[3] = (short) (li[6]);
        data[4] = (short) (li[7]);
        infrared_stereo(data);
    }

    //	{ "隧道有事故，请绕行", "前方施工，请绕行" };
    //  立体显示--路况；
    public void road(int i) {

        data[0] = 0x14;
        data[1] = (short) (i + 0x01);
        infrared_stereo(data);
    }

    //立体显示
    public void infrared_stereo(short[] data) {
        MAJOR = 0x10;
        FIRST = 0xff;
        SECOND = data[0];
        THRID = data[1];
        send();
        yanchi(500);
        MAJOR = 0x11;
        FIRST = data[2];
        SECOND = data[3];
        THRID = data[4];
        send();
        yanchi(500);
        MAJOR = 0x12;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        yanchi(500);
    }

    // 智能交通灯--{"进入识别模式","识别结果为红色，请求确认","识别结果为绿色，请求确认","识别结果为黄色，请求确认"};
    public void Traffic_light(int i) {
        switch (i) {
            case 0:
                traffic_control(0x01, 0x00);
                break;
            case 1:
                traffic_control(0x02, 0x01);
                break;
            case 2:
                traffic_control(0x02, 0x02);
                break;
            case 3:
                traffic_control(0x02, 0x03);
                break;
            default:
                break;
        }
    }

    //智能交通灯
    public void traffic_control(int major, int first) {
        byte type = (byte) TYPE;
        TYPE = 0x0E;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    //{"到达第一层","到达第二层","到达第三层","到达第四层","请求返回车库位于第几层","请求返回前后侧红外状态"};
    public void stereo_garage(int i) {
        switch (i) {
            case 0:  //到达第一层
                garage_control(0x01, 0x01);
                break;
            case 1:  //到达第二层
                garage_control(0x01, 0x02);
                break;
            case 2:  //到达第三层
                garage_control(0x01, 0x03);
                break;
            case 3:  //到达第四层
                garage_control(0x01, 0x04);
                break;
            case 4:  //请求返回车库位于第几层
                garage_control(0x02, 0x01);
                break;
            case 5:  //请求返回前后侧红外状态
                garage_control(0x02, 0x02);
                break;
            default:
                break;
        }
    }

    //立体车库控制
    public void garage_control(int major, int first) {
        byte type = (byte) TYPE;
        TYPE = 0x0D;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    //openmv开启识别二维码
    public void openmv1() {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = (byte) 0x92;
        FIRST = (byte) 0x01;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
        while (mbyteCp[3] != 1 && mbyteCp[3] != 2) {
            Log.e("serial_thread", "openmv1");
            String str = bytesToHexFun2(mbyteCp);
            Log.e("serial_thread", str);
        }
        String str = bytesToHexFun2(mbyteCp);
        String str2 = str.substring(8, 10);  //获取数据长度
        int x = Integer.parseInt(str2, 16) * 2 + 10;  //12 计算数据长度
        openmv1_text = hexStringToString(str.substring(10, x));
        Log.e("serial_thread", str);
        return;
    }

    //openmv关闭识别二维码
    public void openmv2() {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = (byte) 0x92;
        FIRST = (byte) 0x02;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
        Log.e("serial_thread", "关闭二维码成功");
        return;
    }

    //openmv摄像头
    public void opencv_control(int major, int first) {
        byte type = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    // { "开", "关" };
    //闸门 -新
    public void gate_q(int i) {// 闸门
        //(0x01,0x01,0x00,0x00); //开
        //(0x01,0x02,0x00,0x00); //关
        if (i == 0) {
            byte type = (byte) TYPE;
            TYPE = 0x03;
            MAJOR = (byte) 0x01;
            FIRST = (byte) 0x01;
            SECOND = (byte) 0x00;
            THRID = (byte) 0x00;
            send();
            TYPE = type;
        } else if (i == 1) {
            byte type = (byte) TYPE;
            TYPE = 0x03;
            MAJOR = (byte) 0x01;
            FIRST = (byte) 0x02;
            SECOND = (byte) 0x00;
            THRID = (byte) 0x00;
            send();
            TYPE = type;
        }
    }

    // 闸门--旧
    public void gate(int major, int first, int second, int third) {// 闸门
        //(0x01,0x01,0x00,0x00); //开
        //(0x01,0x02,0x00,0x00); //关
        byte type = (byte) TYPE;
        TYPE = 0x03;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = (byte) second;
        THRID = (byte) third;
        send();
        TYPE = type;
    }

    //数码管 Lcd 显示标志物进入计时模式
    public void digital_close() {//数码管关闭
        byte type = (byte) TYPE;
        TYPE = 0x04;
        MAJOR = 0x03;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    public void digital_open() {//数码管打开
        byte type = (byte) TYPE;
        TYPE = 0x04;
        MAJOR = 0x03;
        FIRST = 0x01;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    public void digital_clear() {//数码管清零
        byte type = (byte) TYPE;
        TYPE = 0x04;
        MAJOR = 0x03;
        FIRST = 0x02;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = type;
    }

    String[] dis_item = {"10cm", "20cm", "40cm"};

    //判断LCD要显示的距离后，传入显示代码里。
    public void dig(int i) {
        int k = Integer.parseInt(dis_item[i].substring(0, 2));
        digital_dic(k);
    }

    public void digital_dic(int dis) {//LCD显示标志物第二排显示距离
        byte type = (byte) TYPE;
        int a = 0, b = 0, c = 0;
        a = (dis / 100) & (0xF);
        b = (dis % 100 / 10) & (0xF);
        c = (dis % 10) & (0xF);
        b = b << 4;
        b = b | c;
        TYPE = 0x04;
        MAJOR = 0x04;
        FIRST = 0x00;
        SECOND = (short) (a);
        THRID = (short) (b);
        send();
        TYPE = type;
    }

    //数码管显示 --项目还没有写完
    public void digital(int i, int one, int two, int three) {// 数码管
        byte type = (byte) TYPE;
        TYPE = 0x04;
        if (i == 1) {//数据写入第一排数码管
            MAJOR = 0x01;
            FIRST = (byte) one;
            SECOND = (byte) two;
            THRID = (byte) three;
        } else if (i == 2) {//数据写入第二排数码管
            MAJOR = 0x02;
            FIRST = (byte) one;
            SECOND = (byte) two;
            THRID = (byte) three;
        }
        send();
        TYPE = type;
    }

    public void arm(int MAIN, int KIND, int COMMAD, int DEPUTY) {
        MAJOR = (short) MAIN;
        FIRST = (byte) KIND;
        SECOND = (byte) COMMAD;
        THRID = (byte) DEPUTY;
        send();
    }

    //{"指定显示","上翻一页","下翻一页","自动翻页"};
    //TFT显示器--图片显示模式
    public void TFT_Image(int i) {
        switch (i) {
            case 0:
                LCD_vo_show(1); //指定图片显示-{"1","2","3","4","5"};
                break;
            case 1:
                TFT_LCD(0x10, 0x01, 0x00, 0x00);
                break;
            case 2:
                TFT_LCD(0x10, 0x02, 0x00, 0x00);
                break;
            case 3:
                TFT_LCD(0x10, 0x03, 0x00, 0x00);
                break;
        }
    }

    //指定图片显示
    public void LCD_vo_show(int i) {
        switch (i) {
            case 0:
                TFT_LCD(0x10, 0x01, 0x00, 0x00);
                break;
            case 1:
                TFT_LCD(0x10, 0x02, 0x00, 0x00);
                break;
            case 2:
                TFT_LCD(0x10, 0x03, 0x00, 0x00);
                break;
            case 3:
                TFT_LCD(0x10, 0x04, 0x00, 0x00);
                break;
            case 4:
                TFT_LCD(0x10, 0x05, 0x00, 0x00);
                break;
        }
    }

    // {"A123B4","B567C8","D910E1"};
    //TFT 车牌显示模式
    public void TFT_plate_number(int i) {
        switch (i) {
            case 0:
                TFT_LCD(0x20, 'A', '1', '2');
                yanchi(500);
                TFT_LCD(0x21, '3', 'B', '4');
                break;
            case 1:
                TFT_LCD(0x20, 'B', '5', '6');
                yanchi(500);
                TFT_LCD(0x21, '7', 'C', '8');
                break;
            case 2:
                TFT_LCD(0x20, 'D', '9', '1');
                yanchi(500);
                TFT_LCD(0x21, '0', 'E', '1');
                break;
        }
    }

    //TFT--计时模式--{"开始","关闭","停止"};
    public void TFT_Timer(int i) {
        switch (i) {
            case 0:
                TFT_LCD(0x30, 0x01, 0x00, 0x00);
                break;
            case 1:
                TFT_LCD(0x30, 0x02, 0x00, 0x00);
                break;
            case 2:
                TFT_LCD(0x30, 0x00, 0x00, 0x00);
                break;
        }
    }

    //TFT--距离显示模式--{"100mm","200mm","300mm"};
    public void Distance(int i) {
        if (i == 0) {
            TFT_LCD(0x50, 0x00, 0x01, 0x00);
        }
        if (i == 1) {
            TFT_LCD(0x50, 0x00, 0x02, 0x00);
        }
        if (i == 2) {
            TFT_LCD(0x50, 0x00, 0x03, 0x00);
        }
    }

    //TFT--HEX显示模式--{"0xAA、0x01、0xBB","0xAA、0x02、0x77","0x55、0x03、0x33"};
    public void Hex_show(int i) {
        if (i == 0) {
            TFT_LCD(0x40, 0xAA, 0x01, 0xBB);
        }
        if (i == 1) {
            TFT_LCD(0x40, 0xAA, 0x02, 0x77);
        }
        if (i == 2) {
            TFT_LCD(0x40, 0x55, 0x03, 0x33);
        }
    }

    // TFT显示器
    public void TFT_LCD(int MAIN, int KIND, int COMMAD, int DEPUTY)  //tft lcd
    {
        byte type = (byte) TYPE;
        TYPE = (short) 0x0B;
        MAJOR = (short) MAIN;
        FIRST = (byte) KIND;
        SECOND = (byte) COMMAD;
        THRID = (byte) DEPUTY;
        send();
        TYPE = type;
    }

    //判断要无线充电 是开还是关
    //无线充电--新   //磁悬浮
    public void magnetic(int i) {
        if (i == 0) {
            byte type = (byte) TYPE;
            TYPE = (short) 0x0A;
            MAJOR = (short) 0x01;
            FIRST = (byte) 0x01;
            SECOND = (byte) 0x00;
            THRID = (byte) 0x00;
            send();
            TYPE = type;
        } else if (i == 1) {
            byte type = (byte) TYPE;
            TYPE = (short) 0x0A;
            MAJOR = (short) 0x01;
            FIRST = (byte) 0x02;
            SECOND = (byte) 0x00;
            THRID = (byte) 0x00;
            send();
            TYPE = type;
        }
    }

    //(0x01,0x01,0x00,0x00); 开  (0x01,0x02,0x00,0x00);关
    //无线充电--旧  //磁悬浮
    public void magnetic_suspension(int MAIN, int KIND, int COMMAD, int DEPUTY) //磁悬浮
    {
        byte type = (byte) TYPE;
        TYPE = (short) 0x0A;
        MAJOR = (short) MAIN;
        FIRST = (byte) KIND;
        SECOND = (byte) COMMAD;
        THRID = (byte) DEPUTY;
        send();
        TYPE = type;
    }

    //开始进行语音识别
    public void yuYinKS() {
        MAJOR = (short) 0x90;
        FIRST = (byte) 0x00;
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
        while (mbyteCp[2] != 0x12) {
            Log.e("serial_thread", "语音识别中");
            String str = bytesToHexFun2(mbyteCp);
            Log.e("serial_thread", str);
        }
//        String str = bytesToHexFun2(mbyteCp);
//        Log.e("serial_thread", str);
        YY = mbyteCp[3];
//        Log.e("serial_thread", String.valueOf(YY));

    }

    //发送语音识别结果到控制台
    public void speech(int i) {
        switch (i) {
            case 1:
                //保留
                break;
            case 2:
                //向右转弯
                MAJOR = (byte) 0x02;
                FIRST = (byte) 0x00;
                SECOND = (byte) 0x00;
                THRID = (byte) 0x00;
                send1();
                break;
            case 3:
                //禁止右转
                MAJOR = (byte) 0x03;
                FIRST = (byte) 0x00;
                SECOND = (byte) 0x00;
                THRID = (byte) 0x00;
                send1();
                break;
            case 4:
                //左侧行驶
                MAJOR = (byte) 0x04;
                FIRST = (byte) 0x00;
                SECOND = (byte) 0x00;
                THRID = (byte) 0x00;
                send1();
                break;
            case 5:
                //左行被禁
                MAJOR = (byte) 0x05;
                FIRST = (byte) 0x00;
                SECOND = (byte) 0x00;
                THRID = (byte) 0x00;
                send1();
                break;
            case 6:
                //原地掉头
                MAJOR = (byte) 0x06;
                FIRST = (byte) 0x00;
                SECOND = (byte) 0x00;
                THRID = (byte) 0x00;
                send1();
                break;
            default:
                break;
        }
    }


    // 沉睡
    public void yanchi(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //从string中得到short数据数组
    private short[] StringToBytes(String licString) {
        if (licString == null || licString.equals("")) {
            return null;
        }
        licString = licString.toUpperCase();
        int length = licString.length();
        char[] hexChars = licString.toCharArray();
        short[] d = new short[length];
        for (int i = 0; i < length; i++) {
            d[i] = (short) hexChars[i];
        }
        return d;
    }

    /**
     * 方法二：
     * byte[] to hex string
     *
     * @param bytes
     * @return
     */
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    //    byte转String  将byte删除0x 后全部添加在一起
    public static String bytesToHexFun2(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for (byte b : bytes) { // 利用位运算进行转换，可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }

        return new String(buf);
    }

    private short charToByte(char c) {
        return (short) "0123456789ABCDEF".indexOf(c);
    }

    //字符串转换为short类型，以便wifi传输  String转byte    每两个数字前面加上0x
    private short[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        short[] d = new short[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (short) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    //16进制转为字符串
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
    public int StringToCoordinate(char[] som,String str)
    {
        int d=0,n=0,x=0,b=0,result=0;
        String ss="";
        int j=132,k=0;

        char sh = 0;
        for (int i = 0; i < str.length();i++)
        {
            if(som[1]!='\0')
            {
                ss=str.substring(i+1);
                break;
            }
            char temp = str.charAt(i);
            if((temp>='0'&&temp<='9')||(temp>='A'&&temp<'Z')||(temp>='a'&&temp<'z'))
            {
                som[k++] = temp;
                for (j = i; j<str.length(); j++)
                {
                    char sw = str.charAt(j+1);
                    if((sw>='0'&&sw<='9')||(sw>='A'&&sw<'Z')||(sw>='a'&&sw<'z'))
                    {
                    }else
                    {
                        break;
                    }
                }
                i=j;
            }
        }

        if(!(som[0]>='0'&&som[0]<='9'))
        {
            sh=som[0];
            som[0]=som[1];
            som[1]=sh;
        }

        d=ss.indexOf("东");
        n=ss.indexOf("南");
        x=ss.indexOf("西");
        b=ss.indexOf("北");

        if(d!=-1)
        {
            if(d>x)
            {
                result=1;
            }else
            {
                result=3;
            }
        }
        if(n!=-1)
        {
            if(n>b)
            {
                result=2;
            }else
            {
                result=4;
            }
        }

        return result;
    }

}
